<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="../style/main.css" type="text/css" rel="stylesheet">
        <link href="../style/manager.css" type="text/css" rel="stylesheet">
        <script type="text/javascript" src="../script/jquery-1.8.2.js"></script>
        <script type="text/javascript" src="../script/SMScript.js"></script>
        <title></title>
    </head>
    <body>
        <header>
        <?php
        session_start();
        require_once ('../libchart/classes/libchart.php');
        mysql_connect("localhost:3306", "root", "16886611");
        mysql_select_db("digitalsales");
        
        if(isset($_GET["action"])){
            if($_GET["action"]=="logout"){
               $_SESSION["manager"]=NULL;
               $_SESSION["store"]=NULL;
               header("location:index.php");
            }
        }
        if(!isset($_SESSION["manager"])||$_SESSION["manager"]==NULL){
            echo '<ul>
                 <li>
                    <p>YOU HAVE NOT LOGGED IN YET</p>
                  </li>
                </ul>';
        }else{
            echo '<ul>
                <li id="logout">
                  <p><a href="index.php?action=logout">Log out</a></p>
                </li>
                  <li>
                    <p>Welcome back,'.$_SESSION["manager"].'!</p>
                  </li>
                </ul>';
        }
        
        ?>
        </header>
        <div class="transparentMainpanel">
            <div class="options">
                <?php echo '<h2>At your service, '.$_SESSION["manager"].'</h2>'; ?>
                <div class="optionlist">
                    <table>
                        <tbody>
                            <?php 
                            $selection="option1";
                            if(isset($_GET["option"])){
                               $selection=$_GET["option"];
                            }
                            $addtion1="style='visibility: hidden'";
                            $addtion2="style='visibility: hidden'";
                            $addtion3="style='visibility: hidden'";
                            $textStyle1="style='text-decoration-line:  none'";
                            $textStyle2="style='text-decoration-line:  none'";
                            $textStyle3="style='text-decoration-line:  none'";
                            switch ($selection){
                                case "option1":
                                    $addtion1="style='visibility: visible'";
                                    $textStyle1="style='text-decoration-line:underline'";
                                    break;
                                case "option2":
                                    $addtion2="style='visibility: visible'";
                                    $textStyle2="style='text-decoration-line:underline'";
                                    break;
                                case "option3":
                                    $addtion3="style='visibility: visible'";
                                    $textStyle3="style='text-decoration-line:underline'";
                                    break;
                            }
                            echo   '<tr id="option1"><td><img src="../images/star.gif"  class="optionimg" '.$addtion1.'></td><td '.$textStyle1.'><p class="optiontext" >General Status Report</p></td></tr>
                                    <tr id="option2"><td><img src="../images/star.gif"  class="optionimg" '.$addtion2.'></td><td '.$textStyle2.'><p class="optiontext" >Sales persons performance Report</p></td></tr>
                                    <tr id="option3"><td><img src="../images/star.gif"  class="optionimg" '.$addtion3.'></td><td '.$textStyle3.'><p class="optiontext" >Products Evaluation</p></td></tr>';
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="reportPanel">
                <?php 
                $style1="";
                $style2="style='background-color:activecaption;margin-left:5px'";
                $style3="style='background-color:activecaption;margin-left:5px'";
                $subcondition="Monthly";
                if(isset($_GET["subcondition"])){
                    switch ($_GET["subcondition"]){
                        case "daily":
                            $subcondition="Daily";
                            $style1="style='background-color:activecaption;'";
                            $style2="style='margin-left:5px'";
                            $style3="style='background-color:activecaption;margin-left:5px'";
                            break;
                        case "Monthly":
                            $style1="";
                            $style2="style='background-color:activecaption;margin-left:5px'";
                            $style3="style='background-color:activecaption;margin-left:5px'";
                            break;
                        case "yearly":
                            $subcondition="Yearly";
                            $style1="style='background-color:activecaption;'";
                            $style2="style='background-color:activecaption;margin-left:5px'";
                            $style3="style='margin-left:5px'";
                            break;
                    }
                }
                ?>
               
                        <div class="tagList">
                            <div id="tag1" class="tag" <?php echo $style1;?>>
                                <h2>Monthly<br/>Report</h2>
                            </div>
                            <div id="tag2" class="tag" <?php echo $style2;?>>
                                <h2>Daily<br/>Report</h2>
                            </div>
                            <div id="tag3" class="tag" <?php echo $style3;?>>
                                <h2>Yearly<br/>Report</h2>
                            </div>
                            <div style="clear:both;"></div>
                        </div>
                        
                        <div class="report">
                            <?php 
                            $type="";
                            switch ($selection){
                                case "option1":
                                    $type="General Status";
                                    break;
                                case "option2":
                                    $type="Employee Performance";
                                    break;
                                case "option3":
                                    $type="Products Analyze";
                                    break;
                            }
                            
                            $title=$subcondition." ".$type." Report for ".$_SESSION["store"];
                            echo '<h2>'.$title.'</h2>';

                         
                            $SID =explode("#",$_SESSION['store']);
                            $SID = $SID[1]+1;
                            $description1;
                            
                            if ($type=='General Status'){

                                switch ($subcondition){
                                case "Yearly":
                                    $description1 = " Year ";
                                    $statsql1 = "select YEAR(tr.date), SUM(tr.quantity*(tr.price - ps.cost)) as totalProfit
                                        from product_stores ps , transactions tr where tr.product = ps.PID AND tr.store = ps.SID AND tr.store =".$SID."
                                        AND EXTRACT(YEAR FROM '2012-10-30')-EXTRACT(YEAR FROM tr.date)<=3 group by YEAR(tr.date) ORDER BY MONTH(tr.date) DESC";
                                    break;
                                case "Monthly":
                                    $description1 = " Month ";
                                    $statsql1 = "select MONTH(tr.date), SUM(tr.quantity*(tr.price - ps.cost)) as totalProfit
                                        from product_stores ps , transactions tr where tr.product = ps.PID AND tr.store = ps.SID AND tr.store =".$SID."
                                        AND EXTRACT(MONTH FROM '2012-10-30')-EXTRACT(MONTH FROM tr.date)<=10 group by MONTH(tr.date) ORDER BY MONTH(tr.date) DESC";
                                    
                                    $statsql2 = "select * from (select p.name , SUM(tr.quantity*(tr.price - ps.cost)) as total from products p,product_stores ps , transactions tr where tr.product = ps.PID AND tr.store = ps.SID AND ps.PID = p.PRODUCTID
                                        AND DateDiff ('2012-10-30',tr.date)<=30 AND DateDiff ('2012-10-30',tr.date)>=0 group by p.name)temp ORDER BY total DESC LIMIT 0,10";
                               //    CHANGED HERE!!!!!!!!!!!!!!!!!!!!!!!!!
                                    break;
                                case "Daily":
                                    $description1 = "Day ago from Today";
                                    $statsql1 = "select DateDiff ('2012-10-30',tr.date) as differ , SUM(tr.quantity*(tr.price - ps.cost)) as totalProfit
                                        from product_stores ps , transactions tr where tr.product = ps.PID AND tr.store = ps.SID AND tr.store =".$SID."
                                        AND DateDiff ('2012-10-30',tr.date)<=10 group by DateDiff ('2012-10-30',tr.date) ORDER BY DateDiff ('2012-10-30',tr.date)";
                                    $statsql2 = "select * from (select p.Name ,SUM(tr.quantity*(tr.price - ps.cost)) as total from products p, product_stores ps, transactions tr where tr.product = ps.PID AND tr.store = ps.SID AND ps.PID = p.PRODUCTID
                                        AND DateDiff ('2012-10-30',tr.date)<=10 AND DateDiff ('2012-10-30',tr.date)>=0 group by p.Name)temp ORDER BY total DESC LIMIT 0,10";
                                //    CHANGED HERE!!!!!!!!!!!!!!!!!!!!!!!!!
                                    break;
                                }
                           
                 //-------------------Graph of Past 10 months' profit of certain store     
                           
                                $stats=mysql_query($statsql1);

                                $chart = new VerticalBarChart(650,300);//参数表示需要创建的图像的宽和高
                                $dataSet = new XYDataSet();//实例化一个XY轴数据对象//为这个对象增加四组数据集合，Point对象的第一个参数表示X轴坐标，第二个表示Y轴坐标

                                while ($eachstats=mysql_fetch_row($stats)){

                                    $dataSet->addPoint(new Point($eachstats[0].$description1,ceil($eachstats[1]/1000)));
                                }
                                $chart->setDataSet($dataSet);
                                $chart->setTitle($subcondition." REPORT OF THE PROFIT OF ".$_SESSION["store"]."     Unit:Thousand");
                                $chart->render("../images/HaoChart/temp1.png");  
                                
                                echo '<img src="../images/HaoChart/temp1.png">';
                            
                     //--------------------Graph of the statistics of past 10 months TOP 10 product with the highest profit 
                            
                                if (isset($statsql2)){
                                    $stats=mysql_query($statsql2);

                                    $chart = new VerticalBarChart(650,300);//参数表示需要创建的图像的宽和高
                                    $dataSet = new XYDataSet();//实例化一个XY轴数据对象//为这个对象增加四组数据集合，Point对象的第一个参数表示X轴坐标，第二个表示Y轴坐标

                                    while ($eachstats=mysql_fetch_row($stats)){

                                        $dataSet->addPoint(new Point($eachstats[0],ceil($eachstats[1]/1000)));
                                    }
                                    $chart->setDataSet($dataSet);
                                    $chart->setTitle($subcondition." REPORT OF TOP 10 PRODUCT OF ".$_SESSION["store"]."     Unit:Thousand" );
                                    $chart->render("../images/HaoChart/temp2.png");  
                                    
                                    echo '<img src="../images/HaoChart/temp2.png">';
                                 }
                            }
                            
                            else if ($type=='Employee Performance'){
                                
                                switch ($subcondition){
                                case "Yearly":
                                    
                                    $statsql3 =  "select eAccount.name,sum(tr.price*tr.quantity) from transactions tr  ,(select e.name ,e.account from employees e, salesperson_stores ss ,(select s.StoreID from stores s where s.STOREID = ".$SID.")resStore where resStore.STOREID = ss.store 
                                        and ss.salesperson = e.account) eAccount where tr.salesperson = eAccount.account 
                                        AND DateDiff ('2012-10-30',tr.date)<=365 AND DateDiff ('2012-10-30',tr.date)>=0 
                                        group by eAccount.name";
                                    break;
                                case "Monthly":
                                    
                                    $statsql3 = "select eAccount.name,sum(tr.price*tr.quantity) from transactions tr  ,(select e.name ,e.account from employees e, salesperson_stores ss ,(select s.StoreID from stores s where s.STOREID = ".$SID.")resStore where resStore.STOREID = ss.store 
                                        and ss.salesperson = e.account) eAccount where tr.salesperson = eAccount.account 
                                        AND DateDiff ('2012-10-30',tr.date)<=30 AND DateDiff ('2012-10-30',tr.date)>=0 
                                        group by eAccount.name";
                                    
                                    
                                     $description4 = "Months Ago";
                                    $statsql4 = "select EXTRACT(MONTH FROM '2012-10-30')-EXTRACT(MONTH FROM tr.date) as MonthAgo,sum(tr.price*tr.quantity)
                                        from transactions tr  ,
                                        (select e.account 
                                        from (select * from employees emp where emp.name= 'POS')e, salesperson_stores ss ,
                                        (select s.StoreID from stores s where s.STOREID = ".$SID.")resStore where resStore.STOREID = ss.store 
                                        and ss.salesperson = e.account) eAccount where tr.salesperson = eAccount.account 
                                        AND EXTRACT(MONTH FROM '2012-10-30')-EXTRACT(MONTH FROM tr.date)<=4 
                                        group by MonthAgo";
                                    
                                    break;
                                case "Daily":
                                    
                                    $statsql3 = "select eAccount.name,sum(tr.price*tr.quantity) from transactions tr  ,
                                        (select e.name ,e.account from employees e, salesperson_stores ss ,
                                        (select s.StoreID from stores s where s.STOREID = ".$SID.")resStore 
                                          where resStore.STOREID = ss.store 
                                        and ss.salesperson = e.account) eAccount where tr.salesperson = eAccount.account 
                                        AND DateDiff ('2012-10-30',tr.date)<=10 AND DateDiff ('2012-10-30',tr.date)>=0 
                                        group by eAccount.name";
                                    
                                    $description4 = "Days Ago";
                                    $statsql4 = "select DateDiff ('2012-10-30',tr.date) as DaysAgo,sum(tr.price*tr.quantity) 
                                        from transactions tr  ,
                                        (select e.account from 
                                        (select * from employees emp where emp.name= 'POS')e, 
                                        salesperson_stores ss ,(select s.StoreID from stores s 
                                        where s.STOREID = ".$SID.")resStore where resStore.STOREID = ss.store 
                                        and ss.salesperson = e.account) eAccount where tr.salesperson = eAccount.account 
                                        AND DateDiff ('2012-10-30',tr.date)<=10 AND DateDiff ('2012-10-30',tr.date)>=0
                                        group by DaysAgo";
                                    
                                    break;
                                }
           
                                
                //---------------------relative profit created by each Employee in the past 10/month 
                                $stats=mysql_query($statsql3);

                                $chart = new PieChart(650, 300);
                                $dataSet = new XYDataSet();
                                
                                $employeeNumber=0;
                                while ($eachstats=mysql_fetch_row($stats)){
                                $employeeNumber++;
                                $employeeName[$employeeNumber-1]= $eachstats[0];
                                $dataSet->addPoint(new Point($eachstats[0],ceil($eachstats[1]/1000)));
                                }
                                
                                $chart->setDataSet($dataSet);
                                $chart->setTitle("Monthly Earnings Created by Each Employee In".$_SESSION["store"]."     Unit:Thousand");
                                $chart->render("../images/HaoChart/temp3.png"); 
                                
                                echo '<img src="../images/HaoChart/temp3.png">';
                                
                   //-------------------------the profit trend description of each emoployee
                                
                                if ($subcondition == "Monthly"||$subcondition == "Daily"){
                                $chart = new LineChart(650, 300);
                                $dataSet = new XYSeriesDataSet();
                                 
                                while ($employeeNumber-->0){
                                 
                                $sqlArray= explode("POS", $statsql4);
                                $resSql = $sqlArray[0].$employeeName[$employeeNumber].$sqlArray[1];
                                 
                                
                                $stats=mysql_query($resSql);
                                
                                  

                                    $serie[$employeeNumber] = new XYDataSet();
                                    while ($eachstats=mysql_fetch_row($stats)){
                                        $serie[$employeeNumber]->addPoint(new Point($eachstats[0].$description4,ceil($eachstats[1]/1000)));
                                    }
                                    
                                 $dataSet->addSerie($employeeName[$employeeNumber], $serie[$employeeNumber]);
                                
                                 }
                                 
                                    $chart->setDataSet($dataSet);
                                    
                                    if ($subcondition == "Monthly"){
                                    $chart->setTitle("Each Employee's Performance in the past 4 Months in ".$_SESSION["store"]."     Unit:Thousand");
                                    }
                                    else if ($subcondition =="Daily"){
                                    $chart->setTitle("Each Employee's Performance in the past 10 Days in ".$_SESSION["store"]."     Unit:Thousand");

                                    }
                                    $chart->render("../images/HaoChart/temp4.png"); 

                                    echo '<img src="../images/HaoChart/temp4.png">';
                                }
                            }
                            
                            
                            
                   else if ($type=='Products Analyze'){
                        switch ($subcondition){
                           case "Yearly":
                             $statsql5 = "select from ";
                             break;
                          case "Monthly":
                                    
                             $statsq5 = "";
                              break;
                          case "Daily": 
                             $statsql1 = "";
                             break;
                        }
                                       
                  }
                            
                 
                            
                          
                                
                            ?>
                        </div>
            </div>
        </div>
    </body>
</html>
